<?php
$this->pageTitle = Yii::app()->name . ' - ' . Yii::t('home', 'foretell');
$this->breadcrumbs = array(
    Yii::t('home', 'foretell') => array(Yii::t('app', 'index')),
    Yii::t('foretell', 'update') . ' (' . $order->order_id . ')',
);

if (Yii::app()->user->isGuest) {
    $baseUrl = Yii::app()->getBaseUrl(true);
    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
        'id' => 'mydialog',
        // additional javascript options for the dialog plugin
        'options' => array(
            'title' => Yii::t('home', 'message'),
            'autoOpen' => true,
            'modal' => true,
            'beforeClose' => 'js:function(){window.location = "' . $baseUrl . '/index.php?r=index";}',
            'buttons' => array(
                Yii::t('home', 'close') => 'js:function(){window.location = "' . $baseUrl . '/index.php?r=login";}'
            ),
        ),
    ));

    echo Yii::t('home', 'require_login');

    $this->endWidget('zii.widgets.jui.CJuiDialog');
    return;
}

$baseUrl = Yii::app()->request->baseUrl;
$cs = Yii::app()->clientScript;
$cs->registerCssFile($baseUrl . '/layout/styles/switchery.min.css');
$cs->registerScriptFile($baseUrl . '/layout/scripts/transitionize.min.js');
$cs->registerScriptFile($baseUrl . '/layout/scripts/switchery.min.js');
?>
<div id="focus" class="wrapper row100 bgded" style="background-image:url('images/live-demo/background-03.jpg');">
    <div class="overlay">
        <div id="newsletter" class="clear center">

            <h2 class="uppercase font-x3 btmspace-50"><?= Yii::t('foretell', 'update') ?></h2>

            <div class="form">
                <?php
                $form = $this->beginWidget('CActiveForm', array(
                    'id' => 'foretell-form',
                    'enableClientValidation' => true,
                    'clientOptions' => array(
                        'validateOnSubmit' => true,
                    ),
                    'htmlOptions' => array(
                        'enctype' => 'multipart/form-data'
                    )
                ));
                ?>
                <script type="text/javascript">
//                    $(document).ready(function () {
//                        if ($("#ForetellUpdateForm_package").val() == 3 || $("#ForetellUpdateForm_package").val() == 4) {
//                            $(".picture").show();
//                        } else {
//                            $(".picture").hide();
//                        }
//                    });
                </script>
                <div class="row">
                    <?php
                    $opts = CHtml::listData($package, 'package_id', 'package_name_' . Yii::app()->language);
                    $static = array(
                        '' => Yii::t('foretell', 'package'),
                    );
                    echo $form->dropDownList($model, 'package', $static + $opts, array(
                        'ajax' => array(
                            'type' => 'POST', //request type
                            'url' => CController::createUrl('foretellPrice'),
                            'update' => '#price',
                            'data' => array('package_id' => 'js:this.value'),
                            'success' => 'js:function(data){ '
                            . 'var datas = data.split("|");'
                            . 'document.getElementById("package_desc").innerHTML = datas[1];'
                            . '$("#ForetellUpdateForm_price").val(datas[0]); '
                            . '$("#ForetellUpdateForm_price").attr("placeholder", "' . Yii::t('foretell', 'price') . ' : ' . '"+data+" ' . Yii::t('foretell', 'minimum') . '"); '
                            . '$("#ForetellUpdateForm_price_minimum").val(data[0]); '
                            . '}'
                        ),
                        'options' => array($order->package_id => array('selected' => true))
                            )
                    );
//                echo $form->dropDownList($model, 'package', $opts, array('empty' => ''));
                    ?>
                    <?php echo $form->error($model, 'package'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'price', array('placeholder' => Yii::t('foretell', 'price') . ' : ' . $package_select->price . ' ' . Yii::t('foretell', 'minimum'), 'value' => $order->price)); ?>
                    <?php echo $form->hiddenField($model, 'price_minimum', array('value' => $order->price)); ?>
                    <?php echo $form->error($model, 'price'); ?>
                </div>

                <div class="row">
                    <div id="checkbox-group" class="flex-carousel">
                        <?php // echo $form->labelEx($model, 'foretell_type'); ?>
                        <ul class="flex-carousel-items">
                            <li>
                                <figure class="group">
                                    <figcaption>
                                        <center>
                                            <h2 class="uppercase font-x3 btmspace-50"><?= Yii::t('foretell', 'foretell_type') ?></h2>
                                        </center>
                                    </figcaption>
                                </figure>
                            </li>
                            <?php
                            $chkbox = ForetellTypeUtil::getCheckboxWithValue($form, $model, $order->option_type);
                            for ($i = 0; $i < count($chkbox); $i++) {
                                echo $chkbox[$i];
                            }
                            ?>
                        </ul>
                    </div>
                    <?php echo $form->error($model, 'foretell_type'); ?>
                </div>
                <script type="text/javascript">
                    //                    var elem = document.querySelector('.js-switch');
                    var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

                    elems.forEach(function (html) {
                        new Switchery(html, {size: 'small'});
                    });
                </script>

                <div class="row">
                    <?php echo $form->textField($model, 'firstname', array('value' => $order->name, 'placeholder' => Yii::t('register', 'firstname'))); ?>
                    <?php echo $form->error($model, 'firstname'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'lastname', array('value' => $order->surname, 'placeholder' => Yii::t('register', 'lastname'))); ?>
                    <?php echo $form->error($model, 'lastname'); ?>
                </div>

                <div class="row" style="display: none;">
                    <?php echo $form->textField($model, 'height', array('value' => $order->height, 'placeholder' => Yii::t('register', 'height'))); ?>
                    <?php echo $form->error($model, 'height'); ?>
                </div>

                <div class="row" style="display: none;">
                    <?php echo $form->textField($model, 'weight', array('value' => $order->weight, 'placeholder' => Yii::t('register', 'weight'))); ?>
                    <?php echo $form->error($model, 'weight'); ?>
                </div>

                <div class="row">
                    <?php echo $form->dropDownList($model, 'gender', array('' => Yii::t('register', 'gender'), '1' => Yii::t('register', 'male'), '2' => Yii::t('register', 'female')), array('options' => array($order->sex => array('selected' => true)))); ?>
                    <?php echo $form->error($model, 'gender'); ?>
                </div>

                <div class="row">
                    <?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'attribute' => 'birthday',
                        'model' => $model,
                        'name' => 'birthday',
                        'options' => array(
                            'defaultDate' => ConvertFormatUtil::getStringDateWithoutTime($order->birthdate),
                            'showButtonPanel' => false,
                            'showAnim' => 'slide',
                            'dateFormat' => 'yy-mm-dd',
                            'dayNamesMin' => Yii::t('register', 'short_days'),
                            'monthNamesShort' => Yii::t('register', 'months'),
                            'changeMonth' => true,
                            'changeYear' => true,
                            'yearRange' => '1900:2099',
                            'beforeShow' => 'js:function(){  
                    if($(this).val() != ""){
                        var arrayDate = $(this).val().split("-");  
                        if (parseInt(arrayDate[0]) > 2400) {
                            arrayDate[0] = parseInt(arrayDate[0]) - 543;
                        }
                        $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);
                    }
                    setTimeout(function(){
                        $.each($(".ui-datepicker-year option"), function(j, k){
                            var textYear = "";
                            if(parseInt($(".ui-datepicker-year option").eq(j).val()) < 2400){
                                textYear = parseInt($(".ui-datepicker-year option").eq(j).val()) + 543;
                            }
                            $(".ui-datepicker-year option").eq(j).text(textYear);
                        });             
                    },50);
                }',
                            'afterRender' => 'js:function(){  
                    alert("sss");
                }',
                            'onClose' => 'js:function(){
                    if($(this).val() != "" && $(this).val() == dateBefore){         
                        var arrayDate = dateBefore.split("-");
                        if (parseInt(arrayDate[0]) < 2400) {
                            arrayDate[0] = parseInt(arrayDate[0]) + 543;
                        }
                        $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);    
                    }       
                }',
                            'onSelect' => 'js:function(dateText, inst){ 
                    dateBefore = $(this).val();
                    var arrayDate = dateText.split("-");
                    if (parseInt(arrayDate[0]) < 2400) {
                        arrayDate[0] = parseInt(arrayDate[0]) + 543;
                    }
                    $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);
                }',
                        ),
                        'htmlOptions' => array(
                            'value' => ConvertFormatUtil::ADStringToBEString($order->birthdate, false),
                            'onFocus' => 'this.value = ""',
                            'placeholder' => Yii::t('register', 'birthday')
                        ),
                    ));
                    ?>
                    <?php echo $form->error($model, 'birthday'); ?>
                </div>

                <div class="row">
                    <?php
                    $this->widget('application.extensions.timepicker.EJuiDateTimePicker', array(
                        'model' => $model,
                        'id' => 'ForetellUpdateForm_time_of_birthday',
                        'name' => 'ForetellUpdateForm[time_of_birthday]',
                        'value' => ConvertFormatUtil::getTimeOfDate($order->birthdate, false),
                        'options' => array(
                            'showOn' => 'focus',
                            'timeFormat' => 'hh:mm',
                            'timeOnly' => true,
                        ),
                        'htmlOptions' => array(
                            'maxlength' => '10', // textField maxlength
                            'placeholder' => Yii::t('register', 'time_of_birthday')
                        ),
                    ));
                    ?>
                    <?php echo $form->error($model, 'time_of_birthday'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'mobilephone', array('value' => $order->contact_tel, 'placeholder' => Yii::t('register', 'mobilephone'))); ?>
                    <?php echo $form->error($model, 'mobilephone'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textArea($model, 'remark', array('maxlength' => 300, 'rows' => 6, 'cols' => 50, 'value' => $order->remark, 'placeholder' => Yii::t('foretell', 'remark'))); ?>
                    <?php echo $form->error($model, 'remark'); ?>
                </div>

                <?php if (isset($order->picture) && $order->picture != '') : ?>
                    <div class="row">
                        <?php echo CHtml::link(Yii::t('foretell', 'show'), 'javascript:void(0)', array('id' => 'show_picture', 'onclick' => '$("#picturedialog").dialog("open"); return false;')); ?>
                    </div>
                    <?php
                    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
                        'id' => 'picturedialog',
                        // additional javascript options for the dialog plugin
                        'options' => array(
                            'title' => Yii::t('home', 'message'),
                            'autoOpen' => false,
                            'resizable' => false,
                            'modal' => true,
                            'width' => 'auto',
                            'height' => 'auto'
                        )
                    ));

                    $baseUrl = Yii::app()->getBaseUrl(true);
                    echo '<img id="image" src="' . $baseUrl . '/' . $order->picture . '"/>';

                    $this->endWidget('zii.widgets.jui.CJuiDialog');
                endif;
                ?>
                <div class="row disabled">
                    <? echo Yii::t('admin', 'picture') ?>
                </div>
                <div class="row disabled">
                    <?php echo $form->fileField($model, 'profile_image', array('onchange' => 'js:$("#show_picture").hide()')); ?>
                    <?php echo $form->hiddenField($model, 'profile_image_name', array('value' => $order->picture)); ?>
                    <?php echo $form->error($model, 'profile_image'); ?>
                </div>

                <div class="row">
                    <?php echo $form->error($model, 'validate_order'); ?>
                </div>

                <div class="row buttons">
                    <?php echo CHtml::tag('button', array('class' => 'btn', 'type' => 'submit'), Yii::t('foretell', 'update')); ?>
                    <?php echo CHtml::tag('button', array('class' => 'btn', 'type' => 'reset', 'onclick' => 'js:location.reload()'), Yii::t('foretell', 'reset')); ?>
                    <?php echo CHtml::tag('button', array('class' => 'btn', 'type' => 'back', 'onclick' => 'js:window.location = "' . Yii::app()->getBaseUrl(true) . '/index.php?r=foretell"'), Yii::t('foretell', 'back')); ?>
                </div>

                <?php $this->endWidget(); ?>
            </div><!-- form -->

        </div>
    </div>
</div>